package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service(interfaceClass =CheckItemService.class )
@Transactional
public class CheckItemServiceImp implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void save(CheckItem checkItem) {
        checkItemDao.save(checkItem);
     
    }

    @Override
    public PageResult findByPage(QueryPageBean pageBean) {


        PageHelper.startPage(pageBean.getCurrentPage(),pageBean.getPageSize());
        if(pageBean.getQueryString()==null){
            pageBean.setQueryString("%%");
        }else{
            pageBean.setQueryString("%"+pageBean.getQueryString()+"%");
        }
        Page<CheckItem> byPage = checkItemDao.findByPage(pageBean);
        long total = byPage.getTotal();
        List<CheckItem> result = byPage.getResult();

        return new PageResult(total,byPage.getResult());
    }

    @Override
    public Result deleteById(int id) {

        try {
            checkItemDao.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            System.out.println("cuowu");
        }
        return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    @Override
    public Result findById(int id) {
       CheckItem check = checkItemDao.findById(id);
        System.out.println(check);
        return new Result(true,null,check);
    }

    @Override
    public Result update(@RequestBody CheckItem checkItem) {
        System.out.println("111"+checkItem);
        try {
            checkItemDao.update(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @Override
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemDao.findAll();
        return new Result(true,null,checkItemList);
    }
}
